$().ready(function() {
	var oldStock = 0;
	getTimeList();
	/*times();*/
	loadType();
	validateRule();
});

/*function times() {
	laydate.render({
		elem: '#startAt' //指定元素
		//,format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
		,type: 'datetime'
	});
	laydate.render({
		elem: '#endAt' //指定元素
		,type: 'datetime'
		//,format: 'yyyy-MM-dd HH:mm:ss' //可任意组合
	});
}*/

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/wx/goodsSpike/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			price : {
				required : true,
				min : 0
			},
			stock : {
				required : true,
				max : oldStock,
				min : 0
			},
			rebateOne : {
				required : true,
				min : 0
			},
			rebateTwo : {
				required : true,
				min : 0
			},
			priceC : {
				required : true,
				min : 0
			},
			priceZ : {
				required : true,
				min : 0
			},
			priceG : {
				required : true,
				min : 0
			}
		},
		messages : {
			price : {
				required : icon + "请输入专区原价",
				min : icon + "最小为0"
			},
			stock : {
				required : icon + "请输入库存",
				max : icon + "库存太大",
				min : icon + "最小为0"
			},
			rebateOne : {
				required : icon + "请输入初级分销自反金额",
				min : icon + "最小为0"
			},
			rebateTwo : {
				required : icon + "请输入高级分销自反金额",
				min : icon + "最小为0"
			},
			priceC : {
				required : icon + "请输入专区初级会员价",
				min : icon + "最小为0"
			},
			priceZ : {
				required : icon + "请输入专区中级会员价",
				min : icon + "最小为0"
			},
			priceG : {
				required : icon + "请输入专区高级会员价",
				min : icon + "最小为0"
			}
		}
	})
}

function loadType(){
	console.log("加载商品--------------------")
	var html = "";
	$.ajax({
		url : '/wx/goodsGoods/getGoods',
		success : function(data) {
			// console.log(data);
			data = data.data;
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option data-price="' + data[i].price + '" data-priceC="' + data[i].price_c + '" ' +
					'data-priceZ="' + data[i].price_z + '" data-priceG="' + data[i].price_g + '" data-rebateOne="' + data[i].rebate_one + '" ' +
					'data-rebateTwo="' + data[i].rebate_two + '"data-stock="' + data[i].stock + '" value="' + data[i].id + '">' + data[i].name + '</option>';
				// console.log(data[i])
			}
			// console.log("循环结束");
			$(".chosen-select").append(html);
			$(".chosen-select").chosen({
				maxHeight : 200
			});
			//改变事件
			$('.chosen-select').on('change', function(e, params) {
				var se = $('.chosen-select option:selected');
				console.log("goodsId: "+params.selected);
				console.log("price: "+se.attr("data-price"));
				console.log("stock: "+se.attr("data-stock"));
				$('#one').css("display","");
				$('#oneC').css("display","");
				$('#oneZ').css("display","");
				$('#oneG').css("display","");
				$('#oneCC').css("display","");
				$('#oneGG').css("display","");
				$('#two').css("display","");
				$('#oldPrice').val(se.attr("data-price"));
				$('#oldPriceC').val(se.attr("data-priceC"));
				$('#oldPriceZ').val(se.attr("data-priceZ"));
				$('#oldPriceG').val(se.attr("data-priceG"));
				$('#oldRebateOne').val(se.attr("data-rebateOne"));
				$('#oldRebateTwo').val(se.attr("data-rebateTwo"));
				$('#oldStock').val(se.attr("data-stock"));
				oldStock = se.attr("data-stock");
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

function getTimeList() {
	$.ajax({
		url : "/wx/goodsSpikeTime"+"/getTimeList",
		type : "post",
		data : {
			'delFlag' : '0'
		},
		success : function(r) {
			if (r.code==0) {
				var data = r.data;
				/*console.log("data: "+JSON.stringify(data));
				 console.log("data:" ,data);*/
				for (var i=0; i<data.length; i++) {
					var op = '<option value="'+data[i].id+'">'+data[i].name+'</option>'
					$('#timeId').append(op);
				}

			}else{
				layer.msg(r.msg);
			}
		}
	});
}
